﻿using UnityEngine;
using System.Collections;
using System.Xml.Linq;
using System.IO;

public class GameSettings : MonoBehaviour {
    public static GameSettings Instance;
    public GameObject Player;
    public bool IsPlayerClient = false;
    public bool EnableSyncStrategy = true;
    public string HostIP
    {
        get { return hostIP; }
        set
        {
            hostIP = value;
            SaveHostSetting(settingPath);
        }
    }
    public string HostPort
    {
        get { return hostPort; }
        set
        {
            hostPort = value;
            SaveHostSetting(settingPath);
        }
    }

    string hostIP;
    string hostPort;
    string settingPath;
    void Awake()
    {
        Instance = this;
        settingPath = Application.dataPath + "/host_setting.xml";
        LoadHostSetting(settingPath);
    }

    void LoadHostSetting(string path)
    {
        if(!File.Exists(path))
        {
            hostIP = null;
            hostPort = null;
        }
        else
        {
            XDocument d = XDocument.Load(path);
            XElement setting = d.Element("HostSetting");

            hostIP = setting.Element("HostIP").Value;
            hostPort = setting.Element("HostPort").Value;
        }
    }
    void SaveHostSetting(string path)
    {
        XElement file = new XElement("HostSetting",
            new XElement("HostIP", hostIP),
            new XElement("HostPort", hostPort)
        );

        file.Save(path);
    }
}
